﻿
using RightControl.Common;
using RightControl.IService;
using RightControl.Model;
using RightControl.WebApp.Areas.Admin.Controllers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Web;
using System.Web.Mvc;

namespace RightControl.WebApp.Areas.DataSrv.Controllers
{
    public class PlatFormController : BaseController
    {
        public static IProductService productService { get; set; }
        public static ISubProductImageService subProductImgService { get; set; }
        public static ISubProductSizeService subProductSizeService { get; set; }
        public static IAmazonService amazonService { get; set; }
        public static IDhGateService dhgateService { get; set; }
        public static IFieldMapService fieldMapSvr { get; set; }
        public static IShipService shipService { get; set; }
        public static IProductImgService productImgService { get; set; }
        public static IBaseCategoryService baseCategoryService { get; set; }
        public static IPlatformAuthService platformAuthSvr { get; set; }
        public static ITranslationService translationService { get; set; }
        public static IExportFieldsService exportFieldsService { get; set; }
        public PlatFormController() {
        
        }

        // GET: DataSrv/PlatForm
        public ActionResult Amazon(int? id)
        {
            ViewData["ShipTemplate"] = shipService.GetTemplateList(Operator.UserId, (int)PlatFormEnum.Amazon);
            ViewData["BaseCategory"] = baseCategoryService.GetAllCategory();
            base.Initialize(id);
            return View();
        }

        [HttpGet]
        public string GetFeedRet(int metaId, string feedId) {
            string result = "", feedIdstr = ""; int tryCount = 0;
            if (string.IsNullOrEmpty(feedId)) {
                var model = amazonService.ReadModel(metaId);
                feedIdstr = model.FeedId;
            }
            else {
                feedIdstr = feedId;
            }

            if (!string.IsNullOrEmpty(feedIdstr)) {
                var pAuth = platformAuthSvr.GetAuthByUserId(new PlatformAuth() { UserId = Operator.UserId, PlatForm = (int)PlatFormEnum.Amazon }, new PageInfo(1, 10));
                
            }

            return result;
        }
        
        /// <summary>
        /// 亚马逊列表
        /// </summary>
        /// <param name="filter"></param>
        /// <param name="pageInfo"></param>
        /// <returns></returns>
        [HttpGet]
        public JsonResult AmazonList(PageInfo pageInfo, AmazonMeta filter) {
            filter.CreateBy = Operator.UserId;
            var amazonList = amazonService.GetListByFilter(filter, pageInfo);
            return Json(amazonList, JsonRequestBehavior.AllowGet);
        }


        public ActionResult AmazonEdit(int Id) {
            var model = amazonService.ReadModel(Id);
            return View(model);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult UpdateAmazonMeta(AmazonMeta metaData) {
            bool ret = false;
            if (metaData != null) {
                metaData.UpdateBy = Operator.UserId;
                metaData.UpdateOn = DateTime.Now;
                ret = amazonService.UpdateModel(metaData);
            }
            var result = ret ? SuccessTip() : ErrorTip();
            return Json(result);
        }

        public ActionResult AmazonDelete(int Id) {
            var result = amazonService.DeleteModel(Id) ? SuccessTip() : ErrorTip();
            return Json(result);
        }

        public ActionResult PublishToAmazon(int Id, int PromotionDuration, int ShipTemplateId) {
            var pAuth = platformAuthSvr.GetAuthByUserId(new PlatformAuth() { UserId = Operator.UserId, PlatForm = (int)PlatFormEnum.Amazon }, new PageInfo(1, 10));
            if (pAuth.AuthToken.IsEmpty()) {
                return Json(ErrorTip("请先完成亚马逊平台授权!"));
            }
            var model = amazonService.ReadModel(Id);          
          
            var oriProductDetail = productService.ReadModel(model.ProductId);
            var shipTemplate = shipService.ReadModel(ShipTemplateId);


            var result =  SuccessTip();
       
            return Json(result);
        }

        [HttpPost]
        public ActionResult PublishToDhBatch(List<int> IdList, int PromotionDuration, int ShipTemplateId) {
            if (IdList.IsNotEmpty()) {
                foreach (var item in IdList) {
                    PublishToDh(item, PromotionDuration, ShipTemplateId);
                }
            }
            var result = SuccessTip();

            return Json(result);
        }

        [HttpPost]
        public ActionResult PublishToDh(int Id, int PromotionDuration, int ShipTemplateId) {

            string errMsg = "";
            var model = dhgateService.ReadModel(Id);
            if (model.UpLoadStatus == 2) {
                return Json(ErrorTip("该商品上传中，请勿重复提交!"));
            }

            var oriProductDetail = productService.ReadModel(model.ProductId);
            if (oriProductDetail != null) {
                var shipTemplate = shipService.ReadModel(ShipTemplateId);
                var pAuth = new PlatformAuth();
                if (pAuth != null) {
                   
                }
                else {
                    errMsg = "授权信息获取失败，请稍后重试!";
                }
            }
            else {
                errMsg = "该商品已不存在，请确认后重试!";
            }

            if (string.IsNullOrEmpty(errMsg)) {
                return Json(SuccessTip());
            }
            else {
                return Json(ErrorTip(errMsg));
            }
        }

        // GET: DataSrv/PlatForm
        public ActionResult DhGate(int? id) {
            ViewData["ShipTemplate"] = shipService.GetTemplateList(Operator.UserId, (int)PlatFormEnum.DhGate);
            ViewData["BaseCategory"] = baseCategoryService.GetAllCategory();
            base.Initialize(id);
            return View();
        }

        /// <summary>
        /// 敦煌数据列表
        /// </summary>
        /// <param name="filter"></param>
        /// <param name="pageInfo"></param>
        /// <returns></returns>
        [HttpGet]
        public JsonResult DhGateList(PageInfo pageInfo, DhGateMeta filter) {
            filter.CreateBy = Operator.UserId;
            var dhList = dhgateService.GetListByFilter(filter, pageInfo);
            return Json(dhList, JsonRequestBehavior.AllowGet);
        }

        public ActionResult DhDelete(int Id) {
            var result = dhgateService.DeleteModel(Id) ? SuccessTip() : ErrorTip();
            return Json(result);
        }
    }
}